//
//  UFMultiLineButton.m
//  http://unsolicitedfeedback.com
//
//  Created by Jorge Pedroso <jpedroso@unsolicitedfeedback.com>
//  All code is provided under the New BSD license.
//


//多行显示按钮

#import "UFMultiLineButton.h"

@implementation UFMultiLineButton

- (void)awakeFromNib
{
    UIImage *bg = [[UIImage imageNamed:@"button-gray.png"] 
                   stretchableImageWithLeftCapWidth:10 topCapHeight:20];
    
    [self setBackgroundImage:bg forState:UIControlStateNormal];
    [self setBackgroundImage:bg forState:UIControlStateHighlighted];
    
    [self setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
}

#define TITLE_LABEL_TAG 1234

- (CGRect)titleRectForContentRect:(CGRect)rect
{   
    // define the desired title inset margins based on the whole rect and its padding
    UIEdgeInsets padding = [self titleEdgeInsets];
    CGRect titleRect = CGRectMake(rect.origin.x    + padding.left, 
                                  rect.origin.x    + padding.top, 
                                  rect.size.width  - (padding.right + padding.left), 
                                  rect.size.height - (padding.bottom + padding.top));
       
    // save the current title view appearance
    NSString *title = [self currentTitle];
   // UIColor  *titleColor = [self currentTitleColor];
   // UIColor  *titleShadowColor = [self currentTitleShadowColor];
    
    // we only want to add our custom label once; only 1st pass shall return nil
    UILabel  *titleLabel = (UILabel*)[self viewWithTag:TITLE_LABEL_TAG];
    
    
    if (!titleLabel) 
    {
        // no custom label found (1st pass), we will be creating & adding it as subview
        titleLabel = [[UILabel alloc] initWithFrame:titleRect];
        [titleLabel setTag:TITLE_LABEL_TAG];
        
        // make it multi-line
        [titleLabel setNumberOfLines:0];
        [titleLabel setLineBreakMode:UILineBreakModeWordWrap];
        
        // title appearance setup; be at will to modify
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont:[self.titleLabel font]];
      //  [titleLabel setShadowOffset:CGSizeMake(0, 1)];
        [titleLabel setTextAlignment:UITextAlignmentCenter];
        
        [self addSubview:titleLabel];
        [titleLabel release];
    }
    
    // finally, put our label in original title view's state
    [titleLabel setText:title];
   // [titleLabel setTextColor:titleColor];
  //  [titleLabel setShadowColor:titleShadowColor];
    
    // and return empty rect so that the original title view is hidden
    return CGRectZero;
}


- (void)dealloc {
    [super dealloc];
}


@end
